const router = require('koa-router')()
const db = require('../utils/db')
router.prefix('/classify')

// 修改类别
router.post('/update-classify', async ctx => {
    const { name, id } = ctx.request.body
    const sql = 'update classify set name=? where id=?'
    const r =await db.doSQL(sql, [name, id])
    if (r.affectedRows > 0) {
        ctx.body = db.doResponse({ message: '修改类别成功', data: r })
    } else {
        ctx.body = db.doResponse({ success: false, errorCode: 6003, message: '修改类别失败', data: r })
    }
})

// 删除类别
router.get('/delete-classify', async ctx => {
    const { id } = ctx.query
    const sql = 'delete from classify where id=?'
    const r = await db.doSQL(sql, [id])
    if (r.affectedRows > 0) {
        ctx.body = db.doResponse({ message: '删除类别成功', data: r })
    } else {
        ctx.body = db.doResponse({ success: false, errorCode: 6002, message: '删除类别失败', data: r })
    }
})

// 新增类别
router.post('/add-classify', async ctx => {
    const { name } = ctx.request.body
    const sql = 'insert into classify (name) values (?)'
    const r = await db.doSQL(sql, [name])
    if (r.affectedRows > 0) {
        ctx.body = db.doResponse({ message: '新增类别成功', data: r })
    } else {
        ctx.body = db.doResponse({ success: false, errorCode: 6001, message: '新增类别失败', data: r })
    }
})

// 获取类别
router.get('/get-classify', async ctx => {
    const sql = 'select * from classify'
    const r = await db.doSQL(sql)
    ctx.body = db.doResponse({ message: '获取类别成功', data: r })
})

module.exports = router